package com.biz.primus.ms.promotion.trans;

import com.biz.primus.model.promotion.vo.DiscountPromotionRuleVo;
import com.biz.primus.model.promotion.vo.DiscountPromotionVo;
import com.biz.primus.ms.promotion.dao.po.DiscountPromotion;
import com.google.common.collect.Lists;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;

/**
 * 转换器(DiscountPromotion --> DiscountPromotionVo)
 *
 * @author david-liu
 */
public class DiscountPromotionPo2VoMapper implements Function<DiscountPromotion, DiscountPromotionVo> {
    @Override
    public DiscountPromotionVo apply(DiscountPromotion discountPromotion) {
        DiscountPromotionVo vo = new DiscountPromotionVo();
        vo.setId(discountPromotion.getId());
        vo.setName(vo.getName());
        vo.setDescription(vo.getDescription());
        vo.setChannels(discountPromotion.getChannels());
        vo.setStartTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(discountPromotion.getStartTime()));
        vo.setEndTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(discountPromotion.getEndTime()));
        vo.setEffectMemberLevels(discountPromotion.getEffectMemberLevelStringList());
        vo.setAllowCoupon(discountPromotion.getAllowCoupon());
        vo.setIsStairCut(discountPromotion.getIsStairCut());
        DiscountPromotionRuleVo ruleVo = discountPromotion.getDiscountPromotionRuleVo();
        vo.setLimitType(ruleVo.getLimitType());
        vo.mapIntoRates(ruleVo.getDiscountRates());
        vo.setCommonStatus(discountPromotion.getCommonStatus());
        vo.setCreateTimestamp(discountPromotion.getCreateTimestamp());
        vo.setScope(discountPromotion.getScope());
        vo.setScopeIds(StringUtils.join(discountPromotion.getScopeIds(), ","));
        return vo;
    }

    public List<DiscountPromotionVo> mapWithList(List<DiscountPromotion> discountPromotionList) {
        return Optional.ofNullable(discountPromotionList).orElse(Lists.newArrayList()).stream().map(this::apply).collect(Collectors.toList());
    }

    public DiscountPromotionVo map(DiscountPromotion discountPromotion) {
        return this.apply(discountPromotion);
    }
}
